package com.pkk.spring.cloud.breaker.sentinel.limit;

import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import java.util.ArrayList;
import java.util.List;

/**
 * 简单的限流操作
 *
 * @author peikunkun
 * @version V1.0
 * @date 2020-12-30 21:01
 **/
public class SimpleRuleInitFunc implements InitFunc {

  /**
   * 自定义初始化限流的操作
   *
   * @return void
   * @Param
   * @author peikunkun
   * @date 2020/12/30 0030 下午 9:01
   * @since
   */
  @Override
  public void init() throws Exception {
    //对ping接口拦截qps 一次
    List<FlowRule> rules = new ArrayList<>();
    FlowRule rule = new FlowRule();
    rule.setCount(1);
    rule.setResource("ping");
    rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
    rule.setLimitApp("default");
    rules.add(rule);
    FlowRuleManager.loadRules(rules);

  }
}
